package com.wmm.dao.impl;

import com.wmm.dao.CourseContentDao;
import com.wmm.pojo.Course;
import com.wmm.pojo.Course_Lesson;
import com.wmm.pojo.Course_Section;
import com.wmm.utils.DateUtils;
import com.wmm.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class CourseContentDaoImpl implements CourseContentDao {

    //根据课程ID查询课程相关的信息
    @Override
    public List<Course_Section> findSectionAndLessonByCourseId(int courseId) {
        try {
            //1.创建QueryRunner
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            //2.编写sql
            String sql = "  SELECT\n" +
                    "  id,\n" +
                    "  course_id,\n" +
                    "  section_name,\n" +
                    "  description,\n" +
                    "  order_num,\n" +
                    "  STATUS\n" +
                    "  FROM course_section WHERE course_id = ?";
            //3. 执行查询
            List<Course_Section> sectionList = qr.query(sql, new BeanListHandler<Course_Section>(Course_Section.class),courseId);

            //4. 根据章节ID查询课时信息
            for(Course_Section section : sectionList) {
                //调用方法  回去章节对应的课时
                List<Course_Lesson> lessonList = findLessonBySectionId(section.getId());

                //将课时数据封装到 章节对象中
                section.setCourseLessonList(lessonList);
            }
            return sectionList;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;

        }
    }

    //根据章节ID查询课时相关的信息
    @Override
    public List<Course_Lesson> findLessonBySectionId(int sectionId) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

            String sql = "  SELECT\n" +
                    "  id,course_id,section_id,\n" +
                    "  theme,\n" +
                    "  duration,\n" +
                    "  is_free,\n" +
                    "  order_num,\n" +
                    "  STATUS\n" +
                    "  FROM course_lesson WHERE section_id = ?";
            List<Course_Lesson> lessonList = qr.query(sql, new BeanListHandler<Course_Lesson>(Course_Lesson.class),sectionId);
            return lessonList;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Course findCourseByCourseId(int courseId) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "select id,course_name from course where id = ?";

            Course query = qr.query(sql, new BeanHandler<Course>(Course.class), courseId);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;

        }
    }

    @Override
    public int saveSection(Course_Section section) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

            String sql = "insert into course_section(\n" +
                    "course_id,\n" +
                    "section_name,\n" +
                    "description,\n" +
                    "order_num,\n" +
                    "status,\n" +
                    "create_time,\n" +
                    "update_time) values(?,?,?,?,?,?,?);";
            Object[] params = {
                    section.getCourse_id(),
                    section.getSection_name(),
                    section.getDescription(),
                    section.getOrder_num(),
                    section.getStatus(),
                    section.getCreate_time(),
                    section.getUpdate_time()
            };
            int row = qr.update(sql, params);

            return row;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }


    //修改章节
    @Override
    public int updateSection(Course_Section section) {

        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "UPDATE course_section SET \n" +
                    "section_name = ?,\n" +
                    "description = ?,\n" +
                    "order_num = ?,\n" +
                    "update_time = ? WHERE id = ?";

            Object[] params = {
                    section.getSection_name(),
                    section.getDescription(),
                    section.getOrder_num(),
                    section.getUpdate_time(),
                    section.getId()
            };

            int row = qr.update(sql,params);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;

        }

    }

    //修改章节的状态
    @Override
    public int updateSectionStatus(int id, int status) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "update course_section set status = ?,update_time = ? where id = ?";

            Object[] param = {status, DateUtils.getDateFormart(),id};
            int row = qr.update(sql,param);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;

        }
    }


    //保存课时
    @Override
    public int saveLesson(Course_Lesson lesson) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

            String sql = "  INSERT INTO course_lesson(course_id,\n" +
                    "section_id,\n" +
                    "theme,\n" +
                    "duration,\n" +
                    "is_free,\n" +
                    "order_num) VALUES(?,?,?,?,?,?)";
            Object[] params = {
                    lesson.getCourse_id(),
                    lesson.getSection_id(),
                    lesson.getTheme(),
                    lesson.getDuration(),
                    lesson.getIs_free(),
                    lesson.getOrderNum()
            };
            int row = qr.update(sql, params);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;

        }
    }


    //修改课时
    @Override
    public int updateLesson(Course_Lesson lesson) {


        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "UPDATE course_lesson SET " +
                    "course_id = ?," +
                    "section_id = ?," +
                    "theme = ?," +
                    "duration = ?," +
                    "is_free = ?," +
                    "order_num = ?," +
                    "update_time = ? " +
                    "WHERE id = ?";

            Object[] param = {
                    lesson.getCourse_id(),
                    lesson.getSection_id(),
                    lesson.getTheme(),
                    lesson.getDuration(),
                    lesson.getIs_free(),
                    lesson.getOrderNum(),
                    DateUtils.getDateFormart(),
                    lesson.getId()
            };
            int row = qr.update(sql,param);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;

        }
    }
}
